
#include <stdint.h>
#include "core/api/core_api.h"

namespace {
void MessageCallback(void* service, uint32_t handle, uint32_t session, 
                     const char* data, uint16_t len) {
  char log[1024];
  sprintf(log, "session[%d] : %s", session, data);
  debug_log(log);
  char* data_echo = new char[len];
  strcpy(data_echo, data);
  CoreMessage msg;
  create_message(service, session, true, kLogic, data_echo, len, &msg);
  core_send_by_handle(handle, &msg);
  return;
}

struct TimerData {
  uint32_t session;
  uint32_t counter;
};

void timer_cb(void* service, uint32_t session, void* user_data) {
  TimerData* td = (TimerData*)user_data;
  BOOST_ASSERT(session == td->session);
  char log[128];
  sprintf(log, "timer count %d", td->counter++);
  debug_log(log);
}
}

extern "C" void* Create(void) {
  return 0;
}

extern "C" int32_t Init(void* service, const char* param) {
  core_callback(service, MessageCallback);
  TimerData* td = new TimerData;
  td->counter = 0;
  td->session = add_timer(service, 10, 500, td, timer_cb);
  return 0;
}

extern "C" void Release(void* context) {
}

